package com.ancai.studentmenage;

import com.ancai.fragment.CenterFragment;
import com.ancai.fragment.HomeFragment;
import com.ancai.fragment.MessageFragment;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.os.Build;

public class MainActivity extends ActionBarActivity {
	private RadioButton radiobtn1, radiobtn2, radiobtn3, radiobtn4, radiobtn5;
	FrameLayout frameLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		findView();
		init();
		addListener();

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.frame_1, new HomeFragment()).commit();
		}
	}

	private void findView() {
		radiobtn1 = (RadioButton) findViewById(R.id.radio_1);
		radiobtn2 = (RadioButton) findViewById(R.id.radio_2);
		radiobtn3 = (RadioButton) findViewById(R.id.radio_3);
		radiobtn4 = (RadioButton) findViewById(R.id.radio_4);
		radiobtn5 = (RadioButton) findViewById(R.id.radio_5);
	}

	private void init() {

	}

	private void addListener() {
		radiobtn1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getSupportFragmentManager().beginTransaction()
						.replace(R.id.frame_1, new HomeFragment()).commit();
				radiobtn1.setBackgroundColor(Color.parseColor("#00B366"));
				radiobtn2.setBackgroundColor(Color.parseColor("#ffffff"));
				radiobtn3.setBackgroundColor(Color.parseColor("#ffffff"));
				radiobtn4.setBackgroundColor(Color.parseColor("#ffffff"));
				radiobtn5.setBackgroundColor(Color.parseColor("#ffffff"));
			}
		});
		radiobtn2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getSupportFragmentManager().beginTransaction()
						.replace(R.id.frame_1, new MessageFragment()).commit();
				radiobtn1.setBackgroundColor(Color.parseColor("#ffffff"));
				radiobtn2.setBackgroundColor(Color.parseColor("#00B366"));
				radiobtn3.setBackgroundColor(Color.parseColor("#ffffff"));
				radiobtn4.setBackgroundColor(Color.parseColor("#ffffff"));
				radiobtn5.setBackgroundColor(Color.parseColor("#ffffff"));
			}
		});
		radiobtn3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getSupportFragmentManager().beginTransaction()
						.replace(R.id.frame_1, new CenterFragment()).commit();
				radiobtn1.setBackgroundColor(Color.parseColor("#ffffff"));
				radiobtn2.setBackgroundColor(Color.parseColor("#ffffff"));
				radiobtn3.setBackgroundColor(Color.parseColor("#00B366"));
				radiobtn4.setBackgroundColor(Color.parseColor("#ffffff"));
				radiobtn5.setBackgroundColor(Color.parseColor("#ffffff"));
			}
		});
		radiobtn4.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getSupportFragmentManager().beginTransaction()
						.replace(R.id.frame_1, new CenterFragment()).commit();
				radiobtn1.setBackgroundColor(Color.parseColor("#ffffff"));
				radiobtn2.setBackgroundColor(Color.parseColor("#ffffff"));
				radiobtn3.setBackgroundColor(Color.parseColor("#ffffff"));
				radiobtn4.setBackgroundColor(Color.parseColor("#00B366"));
				radiobtn5.setBackgroundColor(Color.parseColor("#ffffff"));
			}
		});
		radiobtn5.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getSupportFragmentManager().beginTransaction()
						.replace(R.id.frame_1, new CenterFragment()).commit();
				radiobtn1.setBackgroundColor(Color.parseColor("#ffffff"));
				radiobtn2.setBackgroundColor(Color.parseColor("#ffffff"));
				radiobtn3.setBackgroundColor(Color.parseColor("#ffffff"));
				radiobtn4.setBackgroundColor(Color.parseColor("#ffffff"));
				radiobtn5.setBackgroundColor(Color.parseColor("#00B366"));
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			return rootView;
		}
	}

}
